package com.cmc.cloud.cmclink.doc.rpcservice.impl;

import cn.hutool.core.bean.BeanUtil;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.ApiErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.rpcservice.LaneService;
import com.cmc.cloud.cmclink.doc.rpcservice.vo.LaneOneRespVO;
import com.cmc.cloud.cmclink.doc.rpcservice.vo.LaneRespVO;
import com.cmc.cloud.cmclink.doc.rpcservice.vo.LaneVesselRespVO;
import com.cmc.cloud.cmclink.svl.api.base.LaneApi;
import com.cmc.cloud.cmclink.svl.api.base.req.LaneReqDTO;
import com.cmc.cloud.cmclink.svl.api.base.resp.LaneRespDTO;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.CommonResult;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 航线信息查询
 * @author heguo
 */
@Service
public class LaneServiceImpl implements LaneService {

    @Resource
    private LaneApi laneApi;

    @Override
    public List<LaneRespVO> selectLanesByLaneCodes(List<String> laneCodes) {
        if (!CollectionUtils.isEmpty(laneCodes)){
            LaneReqDTO laneReqDto = new LaneReqDTO();
            laneReqDto.setLaneList(laneCodes);
            CommonResult<List<LaneRespDTO>> byLaneCodes = laneApi.getByLaneCodes(laneReqDto);
            if (!ObjectUtils.isEmpty(byLaneCodes)){
                List<LaneRespDTO> checkedData = byLaneCodes.getCheckedData();
                if (!ObjectUtils.isEmpty(checkedData)){
                    List<LaneRespVO> laneRespVos = new ArrayList<>();
                    checkedData.stream().forEach(laneRespDto -> {
                        LaneRespVO laneRespVo = BeanUtil.copyProperties(laneRespDto, LaneRespVO.class);
                        laneRespVos.add(laneRespVo);
                        if(!CollectionUtils.isEmpty(laneRespDto.getLaneList())){
                            laneRespVo.setLaneList(BeanUtil.copyToList(laneRespDto.getLaneList(),LaneRespVO.class));
                        }
                        if(!CollectionUtils.isEmpty(laneRespDto.getLanePartnerList())){
                            laneRespVo.setLanePartnerList(BeanUtil.copyToList(laneRespDto.getLanePartnerList(), LaneOneRespVO.class));
                        }
                        if(!CollectionUtils.isEmpty(laneRespDto.getLaneVesselList())){
                            laneRespVo.setLaneVesselList(BeanUtil.copyToList(laneRespDto.getLaneVesselList(), LaneVesselRespVO.class));
                        }
                    });
                    return laneRespVos;
                };
            }else {
                throw ServiceExceptionUtil.exception(ApiErrorCodeConstants.QUERY_LANE_FAILED);
            }

        }
        return Collections.emptyList();
    }
}
